package cn.csc.module.product.controller.admin.attr;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.product.controller.admin.attr.vo.*;
import cn.csc.module.product.dal.dataobject.attr.AttrDO;
import cn.csc.module.product.convert.attr.AttrConvert;
import cn.csc.module.product.service.attr.AttrService;

@Api(tags = "管理后台 - 属性信息")
@RestController
@RequestMapping("/product/attr")
@Validated
public class AttrController {

    @Resource
    private AttrService attrService;

    @PostMapping("/create")
    @ApiOperation("创建属性信息")
    @PreAuthorize("@ss.hasPermission('product:attr:create')")
    public CommonResult<BigInteger> createAttr(@Valid @RequestBody AttrCreateReqVO createReqVO) {
        return success(attrService.createAttr(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新属性信息")
    @PreAuthorize("@ss.hasPermission('product:attr:update')")
    public CommonResult<Boolean> updateAttr(@Valid @RequestBody AttrUpdateReqVO updateReqVO) {
        attrService.updateAttr(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除属性信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:attr:delete')")
    public CommonResult<Boolean> deleteAttr(@RequestParam("id") BigInteger id) {
        attrService.deleteAttr(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得属性信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:attr:query')")
    public CommonResult<AttrRespVO> getAttr(@RequestParam("id") BigInteger id) {
        AttrDO attr = attrService.getAttr(id);
        return success(AttrConvert.INSTANCE.convert(attr));
    }

    @GetMapping("/list")
    @ApiOperation("获得属性信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:attr:query')")
    public CommonResult<List<AttrRespVO>> getAttrList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<AttrDO> list = attrService.getAttrList(ids);
        return success(AttrConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得属性信息分页")
    @PreAuthorize("@ss.hasPermission('product:attr:query')")
    public CommonResult<PageResult<AttrRespVO>> getAttrPage(@Valid AttrPageReqVO pageVO) {
        PageResult<AttrDO> pageResult = attrService.getAttrPage(pageVO);
        return success(AttrConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出属性信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:attr:export')")
    @OperateLog(type = EXPORT)
    public void exportAttrExcel(@Valid AttrExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttrDO> list = attrService.getAttrList(exportReqVO);
        // 导出 Excel
        List<AttrExcelVO> datas = AttrConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "属性信息.xls", "数据", AttrExcelVO.class, datas);
    }

}
